package cn.didi.feidi.androidorder.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.model.WaterStore;

/**
 * Created by king on 2017/9/3.
 */
public class SearchWaterStoreAdapter extends ArrayAdapter<WaterStore> {

    private int resourceId;

    private String loadText = "加载更多";

    private boolean isMore = true;

    public void setLoadText(String loadText) {
        this.loadText = loadText;
    }

    public void setMore(boolean more) {
        isMore = more;
    }

    public SearchWaterStoreAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<WaterStore> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        WaterStore waterStoreItem = getItem(position); // 获取当前项的BankCard实例

        View view;
        SearchWaterStoreAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new SearchWaterStoreAdapter.ViewHolder();

            viewHolder.waterStoreName = (TextView) view.findViewById(R.id.search_water_store_name);
            viewHolder.waterStoreAddress = (TextView) view.findViewById(R.id.search_water_store_address);

            viewHolder.llDetail = (LinearLayout) view.findViewById(R.id.search_water_store_item_detail);
            viewHolder.tvLoad = (TextView) view.findViewById(R.id.search_water_store_item_load);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (SearchWaterStoreAdapter.ViewHolder) view.getTag(); // 重新获取ViewHolder
        }

        view.setVisibility(View.VISIBLE);
        if (position == getCount() - 1) {
            viewHolder.llDetail.setVisibility(View.GONE);
            if (isMore) {
                viewHolder.tvLoad.setVisibility(View.VISIBLE);
                viewHolder.tvLoad.setText(loadText);
            } else {
                view.setVisibility(View.GONE);
                viewHolder.tvLoad.setVisibility(View.GONE);
            }

        } else {
            viewHolder.llDetail.setVisibility(View.VISIBLE);
            viewHolder.tvLoad.setVisibility(View.GONE);

            viewHolder.waterStoreName.setText(waterStoreItem.getName());
            viewHolder.waterStoreAddress.setText(waterStoreItem.getAddress());
        }

        return view;

    }

    class ViewHolder {
        //        ImageView waterStoreIcon;
        TextView waterStoreName;
        TextView waterStoreAddress;
        //        TextView toWaterStoreDistance;
        LinearLayout llDetail;
        TextView tvLoad;
    }
}